<?php
require_once ('config.php');
require_once ('databasefunctions.php');
require_once ('displayfunctions.php');
validate_user();
html_header();

//Show menu

switch ($_GET['action']) {
	case "add_form" :
	
		title_bar($langclientmaster);	
		?>
		<br><? echo $langoperation . " - " . $langadd ?><br>
		<form method = "GET" action="clientmaster.php">
		<table>
			<tr>
				<td bgcolor='<? echo $color1 ?>'><? echo $langclientid ?>
				<td><input type = 'text' name = 'ClientID' maxlength = '10'>
			<tr>	
				<td bgcolor='<? echo $color1 ?>'><? echo $langclientname ?>
				<td><input type = 'text' name = 'ClientName' maxlength = '50'>
			<tr>
				<td bgcolor='<? echo $color1 ?>'><? echo $langaddressline ?> 1
				<td><input type = 'text' name = 'Address1' maxlength = '50'>
			<tr>
				<td bgcolor='<? echo $color1 ?>'><? echo $langaddressline ?> 2
				<td><input type = 'text' name = 'Address2' maxlength = '50'>
			<tr>
				<td bgcolor='<? echo $color1 ?>'><? echo $langaddressline ?> 3
				<td><input type = 'text' name = 'Address3' maxlength = '50'>
			<tr>
				<td bgcolor='<? echo $color1 ?>'><? echo $langaddressline ?> 4
				<td><input type = 'text' name = 'Address4' maxlength = '50'>
			<tr>
				<td bgcolor='<? echo $color1 ?>'><? echo $langenablestatus ?>
				<td><select name = 'Enable'>
						<option value='0'><? echo $langdisable ?>
						<option value='1'><? echo $langenable ?>
					</select>				
		</table>
		<input type = 'hidden' name = 'action' value = 'add'>
		<input type = 'submit' value = '<? echo $langadd ?>'>		
		</form>
		<?
		break;
		
	case "add" :
		if (empty($_GET['ClientID'])) {
			message_box($langclientid . " " . $langcannotempty);
			?>
				<script>history.go(-1);</script>
			<?
			exit;						
		}
		$sql = "INSERT INTO clientmaster (ClientID, ClientName, Address1, Address2, Address3, Address4,
				Enable)	values (";
		$sql .= "'" . strtoupper($_GET['ClientID']) . "', ";
		$sql .= "'" . $_GET['ClientName'] . "', ";
		$sql .= "'" . $_GET['Address1'] . "', ";
		$sql .= "'" . $_GET['Address2'] . "', ";
		$sql .= "'" . $_GET['Address3'] . "', ";
		$sql .= "'" . $_GET['Address4'] . "', ";
		$sql .= "'" . $_GET['Enable'] . "'";
		$sql .= ")";
		
		$sql_result = sql_execute($sql);
		echo "<br>$langclientid $langadded<br>";
		title_bar($langclientmaster,1);	
		break;
		
	case "modify_form" :
	
		title_bar('Client Master', 1);
		// Obtain values from table
		$sql = "SELECT * FROM clientmaster WHERE ClientID='" . $_GET['ClientID']. "'";
		$sql_result = sql_execute($sql);
		$sql_data = mysql_fetch_array($sql_result);
			
		?>
		<br><? echo $langoperation . " - " . $langmodify ?><br>
		<form method = "GET" action="clientmaster.php">
		<table>
			<tr>
				<td bgcolor='<? echo $color1 ?>'><? echo $langclientid ?>
				<td><input type = 'text' name = 'ClientID' value = '<? echo $_GET['ClientID'] ?>' 
					maxlength = '10' readonly>
			<tr>	
				<td bgcolor='<? echo $color1 ?>'><? echo $langclientname ?>
				<td><input type = 'text' name = 'ClientName' maxlength = '50' value='<?
						echo $sql_data['ClientName'] ?> '>
			<tr>
				<td bgcolor='<? echo $color1 ?>'><? echo $langaddressline ?> 1
				<td><input type = 'text' name = 'Address1' maxlength = '50' value='<?
						echo $sql_data['Address1'] ?> '>				
			<tr>
				<td bgcolor='<? echo $color1 ?>'><? echo $langaddressline ?> 2
				<td><input type = 'text' name = 'Address2' maxlength = '50' value='<?
						echo $sql_data['Address2'] ?> '>				
			<tr>
				<td bgcolor='<? echo $color1 ?>'><? echo $langaddressline ?> 3
				<td><input type = 'text' name = 'Address3' maxlength = '50' value='<?
						echo $sql_data['Address3'] ?> '>				
			<tr>
				<td bgcolor='<? echo $color1 ?>'><? echo $langaddressline ?> 4
				<td><input type = 'text' name = 'Address4' maxlength = '50' value='<?
						echo $sql_data['Address4'] ?> '>
			<tr>
				<td bgcolor='<? echo $color1 ?>'><? echo $langenablestatus ?>
				<td><select name = 'Enable'>
					<?
					if ($sql_data['Enable'] == '0') {
						echo "<option value='0' SELECTED>$langdisable";
						echo "<option value='1'>$langenable";
					}
					else {
						echo "<option value='0'>$langdisable";
						echo "<option value='1' SELECTED>$langenable";
					}
					?>
					</select>				
		</table>
		<input type = 'hidden' name = 'action' value = 'modify'>
		<input type = 'submit' value = '<? echo $langmodify ?>'>		
		</form>
		<?	
		break;
		
	case "modify" :
		$sql = "UPDATE clientmaster SET ";
		$sql .= "ClientName = '" . $_GET['ClientName'] . "', ";
		$sql .= "Address1 = '" . $_GET['Address1'] . "', ";
		$sql .= "Address2 = '" . $_GET['Address2'] . "', ";
		$sql .= "Address3 = '" . $_GET['Address3'] . "', ";
		$sql .= "Address4 = '" . $_GET['Address4'] . "', ";
		$sql .= "Enable = '" . $_GET['Enable'] . "' ";
		$sql .= "WHERE ClientID = '" . $_GET['ClientID'] . "'";
		
		$sql_result = sql_execute($sql);
		echo "<br>$langclientid $langmodified<br>";
		title_bar($langclientmaster, 1);
		break;
		
	case "delete" :
		$sql = "SELECT ClientID FROM billtransaction WHERE ClientID = '" . $_GET['ClientID'] ."' LIMIT 1";
		$sql_result = sql_execute($sql);
		$sql_data = mysql_fetch_array($sql_result);
		if ($sql_data[0]) {
			$message = $langtransactionexist . "<br>" . $langcannotdelclient . "<br>";
			message_box($message);
			?>
				<script>history.go(-1);</script>
			<?
			exit;
		}
		
		$sql = "DELETE FROM clientmaster WHERE ClientID = '" . $_GET['ClientID'] . "'";
		
		$sql_result = sql_execute($sql);
		echo "<br>$langclientid $langdeleted<br>";
		title_bar($langclientmaster, 1);
		break;

	case "count" :
		save_search_list();
		$url_text = retrieve_search_list();
		$sql = select_statement('clientmaster');		
		$sql_result = sql_execute($sql);
		$sql_data = mysql_fetch_array($sql_result);
		$no_of_record = $sql_data[0];
		
		?>
		<script>
		this.location = 'clientmaster.php?action=browse&total_record=<? echo $no_of_record ?>
						&record_offset=0<? echo $url_text ?>'
		</script>
		<?
		break;
					
	case "browse" :
		navigation_bar($langclientmaster, 1);
		column_headings(array($langclientid, $langclientname, $langenablestatus));
		
		// List clients here
		$sql = select_statement('clientmaster',1);
		$sql.= " ORDER BY ClientID LIMIT " . $_GET['record_offset'] . ", " . $_SESSION['RecordPerPage'];
		$sql_result = sql_execute($sql);
		
		while ($sql_data = mysql_fetch_array($sql_result)) {
			$rowcolor = row_color_change();
			echo "\n<tr bgcolor='$rowcolor'>";
			echo "<td>";
			echo "<a href = 'clientmaster.php?action=modify_form&ClientID=".$sql_data['ClientID'];
			echo "'>".$sql_data['ClientID']."</a>";
			echo "<td>";
			echo $sql_data['ClientName'];
			echo "<td>";
			if ($sql_data['Enable'] == 1) echo $langyes;
			else echo $langno;			
			echo "<td>";
			echo "<a href = 'clientmaster.php?action=delete&ClientID=" . 
					$sql_data['ClientID'] ."'>$langdelete</a>";
		}
		?>
		</table>		
		<?
		break;
		
	default :
		title_bar($langclientmaster, 1);
		$field[] = array('title'=>$langclientid, 'field'=>'ClientID', 'length'=>10);
		search_form($field, $_SERVER['PHP_SELF']);
		break;
}

html_footer();
?>